/* #include "threadPool.h" */
#include <unistd.h>
#include <crypt.h>
#include <shadow.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 验证用户身份
int passwd_verify(const char *username, const char *password)
{
  struct spwd *encryPasswd = getspnam(username);
  printf("username: %s\n", encryPasswd->sp_namp);
  printf("password: %s\n", encryPasswd->sp_pwdp);
  char *begin = encryPasswd->sp_pwdp;
  char *end = encryPasswd->sp_pwdp;
  end += strlen(encryPasswd->sp_pwdp);
  while (*begin != '$')
    ++begin;
  while (*end != '$')
    --end;
  int saltLen = end - begin + 1;
  char salt[1024] = {0};
  memcpy(salt, begin, saltLen); // 获取到盐值
  char *encryKey = crypt(password, salt);
  int result = strcmp(encryPasswd->sp_pwdp, encryKey);
  return result;
}

/* int passwd_verify(const char *username, const char *password)
{
  printf("username: %s\n", username);
  printf("password: %s\n", password);

  struct spwd *pwd = getspnam(username);
  if (pwd == NULL)
  {
    printf("账号不存在！\n");
    return 1;
  }
  printf("UserName: %s\n", pwd->sp_namp);
  printf("Password: %s\n", pwd->sp_pwdp);
  return 0;
} */